﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using System.Text;

namespace L2C.Models.Administrator
{
    public class PublishItemModel
    {
        public int ItemId { get; set; }
        public int ProductId { get; set; }
        public string Title { get; set; }
        [AllowHtml]
        public string MainContent { get; set; }
        public string Descriptions { get; set; }
        public int? ViewerCounter { get; set; }
        public int SellPrice { get; set; }
        public short DiscountPercent { get; set; }
        public int ProductPrice { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime? EndDate { get; set; }
        public int? BuyerCounter { get; set; }
        public int? MaximumQuantity { get; set; }
        public int? CategoryId { get; set; }
        public string SmallPicture { get; set; }
        public string ShortTitle { get; set; }
        [AllowHtml]
        public string Highlight { get; set; }
        public string Image { get; set; }
        public string SEOCaption { get; set; }

        public static PublishItemModel Parse(L2C.Publishing_Item publishItem)
        {
            if (publishItem == null) return null;
            PublishItemModel item = new PublishItemModel();
            item.ItemId = publishItem.Item_Id;
            item.ProductId = publishItem.Product_Id;
            item.Title = publishItem.Title;
            item.MainContent = publishItem.MainContent;
            item.Descriptions = publishItem.Descriptions;
            item.ViewerCounter = publishItem.ViewerCounter;
            item.SellPrice = publishItem.SellPrice;
            item.DiscountPercent = publishItem.DiscountPercent;
            item.ProductPrice = publishItem.Product_Price;
            item.StartDate = publishItem.StartDate;
            item.EndDate = publishItem.EndDate;
            item.BuyerCounter = publishItem.BuyerCounter;
            item.MaximumQuantity = publishItem.MaximumQuantity;
            item.CategoryId = publishItem.Category_Id;
            item.SmallPicture = publishItem.SmallPicture;            
            item.ShortTitle = publishItem.ShortTitle; 
            item.Highlight = publishItem.Highlight; 
            item.Image = publishItem.Image;
            item.SEOCaption = publishItem.SEOCaption;

            return item;
        }

        public Publishing_Item PopulateToEntity()
        {            
            Publishing_Item item = new Publishing_Item();
            item.Item_Id = this.ItemId;
            item.Product_Id = this.ProductId;
            item.Title = this.Title;
            item.MainContent = this.MainContent;
            item.Descriptions = this.Descriptions;
            item.ViewerCounter = this.ViewerCounter;
            item.SellPrice = this.SellPrice;
            item.DiscountPercent = this.DiscountPercent;
            item.Product_Price = this.ProductPrice;
            item.StartDate = this.StartDate;
            item.EndDate = this.EndDate;
            item.BuyerCounter = this.BuyerCounter;
            item.MaximumQuantity = this.MaximumQuantity;
            item.Category_Id = this.CategoryId;
            item.SmallPicture = this.SmallPicture;
            item.ShortTitle = this.ShortTitle;
            item.Highlight = this.Highlight;
            item.Image = this.Image;
            item.SEOCaption = Change_AV(this.ShortTitle).Replace(' ','-');

            return item;
        }

        public string Change_AV(string ip_str_change)
        {
            Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
            string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
            char[] BAD_CHARS = new char[] { '!', '@', '#', '$', '%', '_', '"'};
            var result = v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
            result= string.Concat(result.Split(BAD_CHARS, StringSplitOptions.RemoveEmptyEntries)).Replace("  "," ");
            return result;            
        }  
      
    }
}